package com.syedu.day13;

import java.util.Comparator;

/**
 * description：比较器工厂
 * @author Administrator
 * @date 2022/3/29 10:02
 */
public class ComparatorFactory {

    public static final int SAL_MIN_TO_MAX = 1;
    public static final int SAL_MAX_TO_MIN = 2;
    public static final int NAME_MIN_TO_MAX = 3;
    public static final int NAME_MAX_TO_MIN = 4;
    public static final int AGE_MIN_TO_MAX = 5;
    public static final int AGE_MAX_TO_MIN = 6;

    public static Comparator<Employee> getComparator(int type) {
        Comparator<Employee> comparator = null;
        if (type == SAL_MIN_TO_MAX) {
            /**匿名内部类,省掉一个类的定义*/
            comparator = Comparator.comparing(Employee::getSalary);
        } else if (type == SAL_MAX_TO_MIN) {
            comparator = (o1, o2) -> o2.getSalary().compareTo(o1.getSalary());
        } else if (type == NAME_MIN_TO_MAX) {
            comparator = Comparator.comparing(Employee::getName);
        } else if (type == NAME_MAX_TO_MIN) {
            comparator = (o1, o2) -> o2.getName().compareTo(o1.getName());
        } else if (type == AGE_MIN_TO_MAX) {
            comparator = Comparator.comparing(Employee::getAge);
        } else if (type == AGE_MAX_TO_MIN) {
            comparator = (o1, o2) -> o2.getAge().compareTo(o1.getAge());
        } else {
            System.out.println("参数不合法...");
        }
        return comparator;
    }
}
